package com.atguigu.guli.service.trade.controller.api;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.base.utils.JwtHelper;
import com.atguigu.guli.service.base.utils.JwtInfo;
import com.atguigu.guli.service.trade.entity.Order;
import com.atguigu.guli.service.trade.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-08-13
 */
@RestController
@RequestMapping("/api/trade/order")
public class ApiOrderController {

    @Autowired
    OrderService orderService;

    /*
    创建订单
     */
    @GetMapping("/auth/creatOrder/{courseId}")
    public R creatOrder(@PathVariable String courseId,HttpServletRequest request){
        JwtInfo jwtInfo = JwtHelper.getJwtInfo(request);
        String memberId = jwtInfo.getId();
        String orderId = orderService.creatOrder(courseId,memberId);
        return R.ok().data("data",orderId);
    }
    /*
    获取订单信息
     */
    @GetMapping("/auth/getorder/{orderId}")
    public R getorder(@PathVariable String orderId,HttpServletRequest request){
        JwtInfo jwtInfo = JwtHelper.getJwtInfo(request);
        String memberId = jwtInfo.getId();
        Order order = orderService.getById(orderId);
        return R.ok().data("data",order);
    }

    /*
    查询订单状态
     */
    @GetMapping("/auth/getorderStatus/{orderId}")
    public R getorderStaus(@PathVariable String orderId){
        Order order = orderService.getById(orderId);
        return R.ok().data("data",order.getStatus());
    }

    /*
    查询课程是否已经购买
     */
    @GetMapping("/auth/isBuy/{courseId}")
    public R getCourseIsBuy(@PathVariable String courseId,HttpServletRequest request){
        JwtInfo jwtInfo = JwtHelper.getJwtInfo(request);
        String memberId = jwtInfo.getId();
        Order one = orderService.getOne(new LambdaUpdateWrapper<Order>().eq(Order::getCourseId, courseId)
                .eq(Order::getMemberId, memberId)
                .eq(Order::getStatus, 1));
        return R.ok().data("data",one==null?"0":"1");
    }

}

